/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eSales.com.DAL;

import eSales.com.BL.sales.LotsList.SaleItem;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author karamelka
 */
@Entity
@Table(name = "lot")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Lot.findAll", query = "SELECT l FROM Lot l"),
    @NamedQuery(name = "Lot.findByLotId", query = "SELECT l FROM Lot l WHERE l.lotId = :lotId"),
    @NamedQuery(name = "Lot.findByName", query = "SELECT l FROM Lot l WHERE l.name = :name"),
    @NamedQuery(name = "Lot.findByStartPrice", query = "SELECT l FROM Lot l WHERE l.startPrice = :startPrice"),
    @NamedQuery(name = "Lot.findByLastPrice", query = "SELECT l FROM Lot l WHERE l.lastPrice = :lastPrice"),
    @NamedQuery(name = "Lot.findByStartDate", query = "SELECT l FROM Lot l WHERE l.startDate = :startDate"),
    @NamedQuery(name = "Lot.findByEndTime", query = "SELECT l FROM Lot l WHERE l.endTime = :endTime"),
    @NamedQuery(name = "Lot.findByDescription", query = "SELECT l FROM Lot l WHERE l.description = :description"),
    @NamedQuery(name = "Lot.findByType", query = "SELECT l FROM Lot l WHERE l.type = :type"),
    @NamedQuery(name = "Lot.findByBuyer", query = "SELECT l FROM Lot l WHERE l.buyer = :buyer")})
public class Lot implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "lotId")
    private Integer lotId;
    @Size(max = 50)
    @Column(name = "name")
    private String name;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "startPrice")
    private Float startPrice;
    @Column(name = "lastPrice")
    private Float lastPrice;
    @Column(name = "startDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate;
    @Column(name = "endTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endTime;
    @Size(max = 20)
    @Column(name = "buyer")
    private String buyer;
    @Size(max = 10000)
    @Column(name = "description")
    private String description;
    @Size(max = 15)
    @Column(name = "type")
    private String type;
    

    

    public Lot() {
    }

    public Lot(SaleItem item) {
        name = item.getName();
        startPrice = item.getStartPrice();
        description = item.getDiscription();
        type = item.getType();
        try {
            endTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH).parse(item.getEndTime());
            startDate = new Date();
        } catch (ParseException ex) {
            Logger.getLogger(Lot.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public Lot(Integer lotId) {
        this.lotId = lotId;
    }

    public Integer getLotId() {
        return lotId;
    }

    public void setLotId(Integer lotId) {
        this.lotId = lotId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Float getStartPrice() {
        return startPrice;
    }

    public void setStartPrice(Float startPrice) {
        this.startPrice = startPrice;
    }

    public Float getLastPrice() {
        return lastPrice;
    }

    public void setLastPrice(Float lastPrice) {
        this.lastPrice = lastPrice;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getBuyer() {
        return buyer;
    }

    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (lotId != null ? lotId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Lot)) {
            return false;
        }
        Lot other = (Lot) object;
        if ((this.lotId == null && other.lotId != null) || (this.lotId != null && !this.lotId.equals(other.lotId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "eSales.com.DAL.Lot[ lotId=" + lotId + " ] Name: " + this.getName() + "Description: " + this.getDescription();
    }
    

    public void commit() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("eSalesPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(this);
        em.getTransaction().commit();
        em.close();
        emf.close();
    }
}
